package edu.mju.controller;

import com.alibaba.fastjson.JSONObject;
import edu.mju.entity.Order;
import edu.mju.service.IOrderService;
import edu.mju.service.impl.OrderServiceImpl;
import edu.mju.util.Pager;
import edu.mju.util.RequestStrUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author:wyp
 * @Date:2020/5/29 23:32
 */
@WebServlet("/orderController")
public class OrderController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //统一编码，防止中文乱码
        req.setCharacterEncoding("UTF-8");
        //获取method
        String method = req.getParameter("method");

        System.out.println("doget:"+method);
        if("orderList".equals(method)){
            String touristId = req.getParameter("touristId");
            req.getRequestDispatcher("/WEB-INF/view/company/order-list.jsp?touristId="+touristId).forward(req,resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //统一编码，防止中文乱码
        req.setCharacterEncoding("UTF-8");
        //获取method
        String method = req.getParameter("method");

        System.out.println("dopost:"+method);
        if("orderList".equals(method)){
            pagernate(req, resp);
        }else if("orderCanser".equals(method)){
            orderCanser(req,resp);
        }
    }

    /**
     *      删除订单
     * @param req
     * @param resp
     */
    public void orderCanser(HttpServletRequest req, HttpServletResponse resp){
        //获取订单id
        Integer orderId = RequestStrUtil.getValue(req.getParameter("orderId"),0);
        IOrderService orderService = new OrderServiceImpl();

        try {
            if(orderService.deleteOrder(orderId)){
                Order order = new Order();
                order.setUserId(orderId);
                String rs = JSONObject.toJSONString(order);
                resp.getWriter().write(rs);
            }
        } catch (Exception e) {
            System.out.println("pagernate方法跳转失败：");
            e.printStackTrace();
        }
    }

    /**
     *      分页查询所有订单信息
     * @param req
     * @param resp
     */
    public void pagernate(HttpServletRequest req, HttpServletResponse resp){
        resp.setContentType("text/html;charSet=utf-8");
        resp.setHeader("Content-type", "text/html;charset=UTF-8");
        //每页显示数据
        int pageSize = 5 ;
        //设定默认当前页码数
        Integer page = RequestStrUtil.getValue(req.getParameter("page"),0);

        String getSearch = req.getParameter("searchName");

        System.out.println("pagenate1:"+getSearch);




        Order order = new Order();
        Integer touristId = Integer.valueOf(req.getParameter("touristId"));
        order.setTouristId(touristId);
        IOrderService orderService = new OrderServiceImpl();
        Pager pager = orderService.findByTouristId(page,pageSize,touristId);


        req.getSession().setAttribute("pager",pager);

        String result = JSONObject.toJSONString(pager);
        System.out.println("pagernate的result:"+result);

        try {
            resp.getWriter().write(result);
        } catch (Exception e) {
            System.out.println("pagernate方法跳转失败：");
            e.printStackTrace();
        }
    }
}
